<?php
/**
 * Created by PhpStorm.
 * User: 朱杰
 * Date: 2017/2/25
 * Time: 14:39
 */

namespace app\controllers;

use Yii;
use yii\db\Query;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;

class ListController extends Controller
{
    /**
     * @inheritdoc
     */

    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['logout'],
                'rules' => [
                    [
                        'actions' => ['logout'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    /**
     * Displays homepage.
     *
     * @return string
     */
    public function actionFind()
    {
        $session=Yii::$app->session;
        $request=Yii::$app->request;
        $username=$session["username"];

        //  用户的筛选
        if ($request->isPost){
            $type=($request->post("thing-type"))+1;
            $start_time=$request->post("start-time");
            $end_time=$request->post("end-time");
            $key_word=$request->post("key-words");
            $start_time=$start_time?$start_time:'00000000';
            $end_time=$end_time?$end_time:'99999999';
            $key_word=$key_word?$key_word:"%";
            $rows=(new Query())
                ->select(["id","place","time","name","class","publish_time","img_url"])
                ->from("get")
                ->where(["class"=>$type])
                ->andWhere(["between","time",$start_time,$end_time])
                ->andWhere([
                    "or",
                    ["or",
                        ["like","more",$key_word,false],
                        ["like","name",$key_word,false]
                    ],
                    ["or",
                        ["like","qq",$key_word,false],
                        ["like","place",$key_word,false]
                    ]
                ])
                ->orderBy("publish_time desc")
                ->all();
        }else{
            $rows=(new Query())
                ->select(["id","place","time","name","class","publish_time","img_url"])
                ->from("get")
                ->orderBy("publish_time desc")
                ->all();
        }



// 获取失物类别
        $types=(new Query())
            ->select(["name"])
            ->from("types")
            ->orderBy("id")
            ->all();

        return $this->render('find',["finds"=>$rows,"types"=>$types]);
    }

    public function actionLost()
    {
        $request=Yii::$app->request;
        $session=Yii::$app->session;
        $username=$session["username"];

        //  用户的筛选
        if ($request->isPost){
            $type=($request->post("thing-type"))+1;
            $start_time=$request->post("start-time");
            $end_time=$request->post("end-time");
            $key_word=$request->post("key-words");
            $start_time=$start_time?$start_time:'00000000';
            $end_time=$end_time?$end_time:'99999999';
            $key_word=$key_word?$key_word:"%";
            $rows=(new Query())
                ->select(["id","place","time","name","class","publish_time","img_url"])
                ->from("find")
                ->where(["class"=>$type])
                ->andWhere(["between","time",$start_time,$end_time])
                ->andWhere([
                              "or",
                              ["or",
                                  ["like","more",$key_word,false],
                                  ["like","name",$key_word,false]
                              ],
                              ["or",
                                  ["like","qq",$key_word,false],
                                  ["like","place",$key_word,false]
                              ]
                          ])
                ->orderBy("publish_time desc")
                ->all();
        }else{
            $rows=(new Query())
                ->select(["id","place","time","name","class","publish_time","img_url"])
                ->from("find")
                ->orderBy("publish_time desc")
                ->all();
        }
// 获取失物类别
        $types=(new Query())
            ->select(["name"])
            ->from("types")
            ->orderBy("id")
            ->all();
            
        return $this->render('lost',["lost"=>$rows,"types"=>$types]);
    }

    /**
     * Login action.
     *
     * @return string
     */

    public function actionLogin()
    {
        if (!Yii::$app->user->isGuest) {
            return $this->goHome();
        }

        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->goBack();
        }
        return $this->render('login', [
            'model' => $model,
        ]);
    }

    /**
     * Logout action.
     *
     * @return string
     */
    public function actionLogout()
    {
        Yii::$app->user->logout();

        return $this->goHome();
    }

    /**
     * Displays contact page.
     *
     * @return string
     */
    public function actionContact()
    {
        $model = new ContactForm();
        if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
            Yii::$app->session->setFlash('contactFormSubmitted');

            return $this->refresh();
        }
        return $this->render('contact', [
            'model' => $model,
        ]);
    }

    /**
     * Displays about page.
     *
     * @return string
     */
    public function actionAbout()
    {
        return $this->render('about');
    }
}
